﻿package mbalib.ui.core
{
	import mbalib.ui.events.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.system.*;
	import flash.text.*;
	import flash.utils.*;
	
	public class BUI extends Sprite
	{
		protected var _x:Number;
		protected var _y:Number;
		protected var _width:Number;
		protected var _height:Number;
		protected var _enabled:Boolean;
		protected var instanceStyles:Object;
		protected var callLaterMethods:Dictionary;
		protected var invalidHash:Object;
		public static var defaultStyles:Object = {textFormat:new TextFormat("宋体", 12, 0, false, false, false, "", "", "left", 0, 0, 0, 0), disabledTextFormat:new TextFormat("宋体", 12, 10066329, false, false, false, "", "", "left", 0, 0, 0, 0)};
		public static var inCallLaterPhase:Boolean = false;
		
		public function BUI()
		{
			this.instanceStyles = {};
			this.invalidHash = {};
			this.callLaterMethods = new Dictionary();
			this._enabled = true;
			this.initUI();
			this.invalidate(Invalidation.ALL);
			return;
		}// end function
		
		public function invalidate(param1:String = "all", param2:Boolean = true) : void
		{
			this.invalidHash[param1] = true;
			if (param2)
			{
				this.callLater(this.draw);
				//this.draw();
			}
			return;
		}// end function
		
		public function setPosition(param1:Number, param2:Number, param3:Boolean = true) : void
		{
			if (isNaN(param1) || isNaN(param2))
			{
				return;
			}
			this._x = param1;
			this._y = param2;
			super.x = Math.round(param1);
			super.y = Math.round(param2);
			if (param3)
			{
				dispatchEvent(new BUIEvent(BUIEvent.POSITION_CHANGE));
			}
			return;
		}// end function
		
		override public function get x() : Number
		{
			return this._x;
		}// end function
		
		override public function set x(param1:Number) : void
		{
			this.setPosition(param1, this._y);
			return;
		}// end function
		
		override public function get y() : Number
		{
			return this._y;
		}// end function
		
		override public function set y(param1:Number) : void
		{
			this.setPosition(this._x, param1);
			return;
		}// end function
		
		public function setSize(param1:Number, param2:Number, param3:Boolean = true) : void
		{
			if (this._width == param1 && this._height == param2)
			{
				return;
			}
			this._width = param1;
			this._height = param2;
			this.invalidate(Invalidation.SIZE);
			if (param3)
			{
				dispatchEvent(new BUIEvent(BUIEvent.SIZE_CHANGE, false));
			}
			return;
		}// end function
		
		override public function get width() : Number
		{
			return this._width;
		}// end function
		
		override public function set width(param1:Number) : void
		{
			if (this._width == param1)
			{
				return;
			}
			this.setSize(param1, this._height);
			return;
		}// end function
		
		override public function get height() : Number
		{
			return this._height;
		}// end function
		
		override public function set height(param1:Number) : void
		{
			if (this._height == param1)
			{
				return;
			}
			this.setSize(this._width, param1);
			return;
		}// end function
		
		public function get enabled() : Boolean
		{
			return this._enabled;
		}// end function
		
		public function set enabled(param1:Boolean) : void
		{
			if (param1 == this._enabled)
			{
				return;
			}
			this._enabled = param1;
			this.invalidate(Invalidation.STATE);
			return;
		}// end function
		
		public function drawNow() : void
		{
			this.draw();
			return;
		}// end function
		
		public function get classStyles() : Object
		{
			return defaultStyles;
		}// end function
		
		//param2必须是文本形式的
		public function setStyle(param1:String, param2:Object) : void
		{
			if (this.instanceStyles[param1] === param2 && !(param2 is TextFormat))
			{
				return;
			}
			this.instanceStyles[param1] = param2;//把MC放到上面
			this.invalidate(Invalidation.STYLES);
			return;
		}// end function
		
		public function setStyles(param1:Object) : void
		{
			var _loc_2:String = null;
			for (_loc_2 in param1)
			{
				
				this.setStyle(_loc_2, param1[_loc_2]);
			}
			return;
		}// end function
		
		public function getStyle(param1:String) : Object
		{
			return this.instanceStyles[param1];
		}// end function
		
		public function clearStyle(param1:String) : void
		{
			this.setStyle(param1, null);
			return;
		}// end function
		
		protected function initUI() : void
		{
			this.setPosition(0, 0);
			return;
		}// end function
		
		protected function draw() : void
		{
			this.validate();
			return;
		}// end function
		
		protected function validateNow() : void
		{
			this.invalidate(Invalidation.ALL, false);
			this.draw();
			return;
		}// end function
		
		protected function isInvalid(param1:String, ... args) : Boolean
		{
			if (this.invalidHash[param1] || this.invalidHash[Invalidation.ALL])
			{
				return true;
			}
			while (args.length > 0)
			{
				
				if (this.invalidHash[args.pop()])
				{
					return true;
				}
			}
			return false;
		}// end function
		
		protected function validate() : void
		{
			this.invalidHash = {};
			return;
		}// end function
		
		protected function callLater(param1:Function) : void
		{
			var _loc_2:String = null;
			if (inCallLaterPhase)
			{
				return;
			}
			
			this.callLaterMethods[param1] = true;
			if (stage != null)
			{
				_loc_2 = Capabilities.os.toLowerCase().indexOf("linux") != -1 ? (Event.ENTER_FRAME) : (Event.RENDER);
			 
				stage.addEventListener(_loc_2, this.callLaterDispatcher, false, 0, true);
				if (_loc_2 == Event.RENDER)
				{
					stage.invalidate();
				}
			}
			else
			{
				addEventListener(Event.ADDED_TO_STAGE, this.callLaterDispatcher, false, 0, true);
			}
			return;
		}// end function
		
		private function callLaterDispatcher(event:Event) : void
		{
			var _loc_2:String = null;
			var _loc_4:Object = null;
			if (event.type == Event.ADDED_TO_STAGE)
			{
				removeEventListener(Event.ADDED_TO_STAGE, this.callLaterDispatcher);
				_loc_2 = Capabilities.os.toLowerCase().indexOf("linux") != -1 ? (Event.ENTER_FRAME) : (Event.RENDER);
				stage.addEventListener(_loc_2, this.callLaterDispatcher, false, 0, true);
				stage.invalidate();
				return;
			}
			else
			{
				_loc_2 = Capabilities.os.toLowerCase().indexOf("linux") != -1 ? (Event.ENTER_FRAME) : (Event.RENDER);
				event.target.removeEventListener(_loc_2, this.callLaterDispatcher);
				if (stage == null)
				{
					addEventListener(Event.ADDED_TO_STAGE, this.callLaterDispatcher, false, 0, true);
					return;
				}
			}
			inCallLaterPhase = true;
			var _loc_3:* = this.callLaterMethods;
			for (_loc_4 in _loc_3)
			{
				 
				 _loc_4();
				delete _loc_3[_loc_4];
			}
			inCallLaterPhase = false;
			return;
		}// end function
		
		protected function getStyleValue(param1:String)
		{
		 
			return this.instanceStyles[param1] == null ? (this.classStyles[param1]) : (this.instanceStyles[param1]);
		}// end function
		
		public static function mergeStyles(... args) : Object
		{
			 var _loc_5:Object = null;
            var _loc_6:String = null;
            //args = {};
            var _loc_3:* = args.length;
            var _loc_4:uint = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_5 = args[_loc_4];
                for (_loc_6 in _loc_5)
                {
                    
                    args[_loc_6] = args[_loc_4][_loc_6];
                }
                _loc_4 = _loc_4 + 1;
            }
            return args;
		}// end function
		
		public static function getSkinInstance(param1) : DisplayObject
		{
			
			var skin:* = param1;
			 
			if (skin == null)
			{
				return null;
			}
			if (skin is Class)
			{
				return new skin as DisplayObject;
			}
			if (skin is DisplayObject)
			{
				(skin as DisplayObject).x = 0;
				(skin as DisplayObject).y = 0;
				return skin as DisplayObject;
			}
			var classDef:Class;
		
			try
			{
				 
				classDef = ApplicationDomain.currentDomain.getDefinition(skin) as Class;
			}
			catch (e:Error)
			{
				classDef;
			}
			if (classDef == null)
			{
				return null;
			}
			return new classDef as DisplayObject;
		}// end function
		
	}
}
